package com.baidu.fenfayun.demo.act;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.BindView;
import com.baidu.yun.gameline.R;
import com.baidu.fenfayun.demo.auth.*;
import com.baidu.fenfayun.demo.home.*;
import com.baidu.fenfayun.demo.model.*;
import com.baidu.fenfayun.demo.base.*;
import android.support.v4.app.Fragment;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.baidu.fenfayun.demo.util.*;


public class SignupAct extends BaseFragmentActivity implements SmartLoginCallbacks{
   private static final String TAG = "SignupActivity";

   @BindView(R.id.input_name) EditText _nameText;
   @BindView(R.id.input_email) EditText _emailText;
   @BindView(R.id.input_mobile) EditText _mobileText;
   @BindView(R.id.input_password) EditText _passwordText;
   @BindView(R.id.input_reEnterPassword) EditText _reEnterPasswordText;
   @BindView(R.id.btn_signup) QMUIRoundButton _signupButton;
   @BindView(R.id.link_login) TextView _loginLink;

   SmartUser currentUser;
   SmartLoginConfig config;
   SmartLogin smartLogin;

   QMUITipDialog tipDialog;
   @Override
   protected int getContextViewId() {
      return R.id.signmain;
   }

   @Override
   public void onResume()
   {
      super.onResume();
      hideKeyBoard();
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_signup);
      ButterKnife.bind(this);
      config = new SmartLoginConfig(getApplicationContext(), this);

      this.getWindow().setSoftInputMode(android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      _signupButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            signup();
         }
      });

      _loginLink.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // Finish the registration screen and return to the Login activity
            Intent intent = new Intent(getApplicationContext(),LoginAct.class);
            startActivity(intent);
            overridePendingTransition(com.qmuiteam.qmui.R.anim.slide_in_left, com.qmuiteam.qmui.R.anim.slide_out_left);
         }
      });
   }

   public void signup() {
      Log.d(TAG, "Signup");

      if (!validate()) {
         onValidateFailed(); 
         return;
      }

      _signupButton.setEnabled(false);

      tipDialog = new QMUITipDialog.Builder(this)
         .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
         .setTipWord("正在加载")
         .create(); 
      tipDialog.show();


      // TODO: Implement your own signup logic here.

      smartLogin = SmartLoginFactory.build(LoginType.CustomLogin);
      smartLogin.signup(config);

   }




   public boolean validate() {
      boolean valid = true;

      String name = _nameText.getText().toString();
      String address = " " ;
      String email = _emailText.getText().toString();
      String mobile = _mobileText.getText().toString();
      String password = _passwordText.getText().toString();
      String reEnterPassword = _reEnterPasswordText.getText().toString();

      if (name.isEmpty() || name.length() < 3) {
         _nameText.setError("至少3个字符");
         valid = false;
      } else {
         _nameText.setError(null);
      }

      if (address.isEmpty()) {
         //_addressText.setError("Enter Valid Address");
         valid = false;
      } else {
         //_addressText.setError(null);
      }


      if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
         _emailText.setError("请输入有效的邮箱");
         valid = false;
      } else {
         _emailText.setError(null);
      }

      if (mobile.isEmpty() || mobile.length()!=11) {
         _mobileText.setError("请输入有效手机号");
         valid = false;
      } else {
         _mobileText.setError(null);
      }

      if (password.isEmpty() || password.length() < 5 || password.length() > 10) {
         _passwordText.setError("5到10个有效的字符或数字");
         valid = false;
      } else {
         _passwordText.setError(null);
      }

      if (reEnterPassword.isEmpty() || reEnterPassword.length() < 4 || reEnterPassword.length() > 10 || !(reEnterPassword.equals(password))) {
         _reEnterPasswordText.setError("两次密码不匹配");
         valid = false;
      } else {
         _reEnterPasswordText.setError(null);
      }

      return valid;
   }
   @Override
   public void onLoginFailure(SmartLoginException e) {
      _signupButton.setEnabled(true);
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
      tipDialog.dismiss();
      DialogUtil.show(this,"操作失败",DialogUtil.Stype.FAIL);
   }
   @Override
   public void onLoginSuccess(SmartUser user) {
      Toast.makeText(this, user.toString(), Toast.LENGTH_SHORT).show();
      _signupButton.setEnabled(true);
      tipDialog.dismiss() ;
      setResult(RESULT_OK, null);
      goHome();
   }

   public void onValidateFailed() {
      Toast.makeText(getBaseContext(), "登录失败", Toast.LENGTH_LONG).show();
      _signupButton.setEnabled(true);
   }

   @Override
   public SmartUser doCustomLogin() {
      return null;
   }

   @Override
   public SmartUser doCustomSignup() {
      String name = _nameText.getText().toString();
      String email = _emailText.getText().toString();
      String mobile = _mobileText.getText().toString();
      String password = _passwordText.getText().toString();
      String reEnterPassword = _reEnterPasswordText.getText().toString();

      UserDB udb = new UserDB();
      udb.create(email,password,email,"");

      SmartUser user = new SmartUser();
      user.setEmail(email.toString());
      return user;
   }
   public void goHome(){

      BaseFragment fragment = new HomeFragment();
      startFragment(fragment);

   }
   @Override
   public void onBackPressed() {
      if (getSupportFragmentManager().getBackStackEntryCount() <= 1 ) {
         moveTaskToBack(true);
      } else {
         super.onBackPressed();
      } 
   }


}
